package com.sihan.framework.etm.controller.business;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.CommonBiz;
import com.sihan.framework.etm.dto.business.BaseDTO;
import com.sihan.framework.etm.dto.business.ToolOverviewDTO;
import com.sihan.framework.etm.enums.MqttSendCommandFunc;
import com.sihan.framework.etm.service.business.CheckService;
import com.sihan.framework.etm.vo.business.BaseToolInfo;
import com.sihan.framework.etm.vo.business.ToolOverviewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author gyz
 * @version 1.0
 * @since jdk1.8
 */
@Api(tags = "检验与试验")
@RestController
@RequestMapping("/bs/v0/inspect")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InspectController {

	private final CheckService checkService;
	
	private final CommonBiz commonBiz;
	
	@ApiOperation(value = "验证是否有检验出库权限", notes = "验证是否有检验出库权限")
	@GetMapping("/permission/has_out")
	@BusinessLog("验证是否有检验出库权限")
	@RequiresPermissions("bs:inspect:out")
	public ApiResult<?> validateHasOutPermission(){
		commonBiz.sendCommandToDevice(MqttSendCommandFunc.COMMAND_2000.getValue());
		return ApiResult.success();
	}
	
	@ApiOperation(value = "验证是否有检验入库权限", notes = "验证是否有检验入库权限")
	@GetMapping("/permission/has_return")
	@BusinessLog("验证是否有检验入库权限")
	@RequiresPermissions("bs:inspect:in")
	public ApiResult<?> validateHasInPermission(){
		commonBiz.sendCommandToDevice(MqttSendCommandFunc.COMMAND_2000.getValue());
		return ApiResult.success();
	}

	@ApiOperation(value = "检验提醒", notes = "检验提醒", response = ToolOverviewVO.class)
	@GetMapping("/getToolList")
	@BusinessLog(value = "检验提醒")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "toolCode", value = "RFID编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "deviceId", value = "智能柜编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "modelId", value = "型号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "checkStartDate", value = "检验到期开始时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "checkEndDate", value = "检验到期结束时间", dataType = "string", paramType = "query")
	})
	@RequiresPermissions("bs:inspect:remind")
	public ApiResult<?> getToolList(ToolOverviewDTO dto){
		return ApiResult.success(checkService.getToolList(dto));
	}

	@ApiOperation(value = "检验台账", notes = "检验台账", response = ToolOverviewVO.class)
	@GetMapping("/getToolCheckList")
	@BusinessLog(value = "检验台账")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "toolCode", value = "RFID编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "deviceId", value = "智能柜编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "modelId", value = "型号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "checkStartDate", value = "检验到期开始时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "checkEndDate", value = "检验到期结束时间", dataType = "string", paramType = "query")
	})
	@RequiresPermissions("bs:inspect:standing")
	public ApiResult<?> getToolCheckList(ToolOverviewDTO dto){
		return ApiResult.success(checkService.getToolCheckList(dto));
	}

	@ApiOperation(value = "更改提醒周期", notes = "更改提醒周期")
	@PostMapping("/updateRemindCycle")
	@BusinessLog(value = "更改提醒周期")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "remindCycle", value = "输入周期值", dataType = "string", paramType = "query", required = true)
	})
	@RequiresPermissions("bs:inspect:remind")
	public ApiResult<?> updateRemindCycle(@RequestBody JSONObject jsonObject){
		checkService.updateRemindCycle((String) jsonObject.get("remindCycle"));
		return ApiResult.success();
	}

	@ApiOperation(value = "检验出库统计", notes = "检验出库统计", response = Map.class)
	@GetMapping("/getCheckToolOutStatistical")
	@BusinessLog(value = "检验出库统计")
	@RequiresPermissions("bs:inspect:out")
	public ApiResult<?> getCheckToolOutStatistical(){
		return ApiResult.success(checkService.getCheckToolOutStatistical());
	}

	@ApiOperation(value = "检查出库清单", notes = "检查出库清单", response = BaseToolInfo.class)
	@GetMapping("/getToolCheckOutList")
	@BusinessLog(value = "检查出库清单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)
	})
	@RequiresPermissions("bs:inspect:out")
	public ApiResult<?> getToolCheckOutList(BaseDTO dto){
		return ApiResult.success(checkService.getToolCheckOutList(dto));
	}
	
	@ApiOperation(value = "检验出库确认", notes = "检验出库确认", response = ApiResult.class)
	@GetMapping("/out/confirm")
	@BusinessLog("检验出库确认")
	@RequiresPermissions("bs:inspect:out")
	public ApiResult<?> outConfirmBorrow(String mechanismId,String inspectDate){
		return checkService.outConfirmBorrow(mechanismId,inspectDate);
	}

	@ApiOperation(value = "检查提醒清单", notes = "检查提醒清单", response = BaseToolInfo.class)
	@GetMapping("/getToolNeedCheckList")
	@BusinessLog(value = "检查提醒清单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)
	})
	@RequiresPermissions("bs:inspect:out")
	public ApiResult<?> getToolNeedCheckList(BaseDTO dto){
		return ApiResult.success(checkService.getToolNeedCheckList(dto));
	}

	@ApiOperation(value = "检验入库统计", notes = "检验入库统计", response = Map.class)
	@GetMapping("/getCheckToolInStatistical")
	@BusinessLog(value = "检验入库统计")
	@RequiresPermissions("bs:inspect:in")
	public ApiResult<?> getCheckToolInStatistical(){
		return ApiResult.success(checkService.getCheckToolInStatistical());
	}

	@ApiOperation(value = "检验入库清单", notes = "检验入库清单", response = Map.class)
	@GetMapping("/getCheckToolInList")
	@BusinessLog(value = "检验入库清单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)
	})
	@RequiresPermissions("bs:inspect:in")
	public ApiResult<?> getCheckToolInList(BaseDTO dto){
		return ApiResult.success(checkService.getCheckToolInList(dto));
	}
	
	@ApiOperation(value = "检验入库确认", notes = "检验入库确认", response = ApiResult.class)
	@GetMapping("/in/confirm")
	@BusinessLog("检验入库确认")
	@RequiresPermissions("bs:inspect:in")
	public ApiResult<?> inConfirmBorrow(@ApiParam(name = "checkDate", value = "检验日期", required = false) 
		@RequestParam(name="checkDate",required=false) String checkDate){
		return checkService.inConfirmBorrow(checkDate);
	}

//	@ApiOperation(value = "操作异常清单", notes = "操作异常清单")
//	@GetMapping("/addException")
//	@BusinessLog(value = "操作异常清单")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "toolId", value = "工器具Id", dataType = "String", paramType = "query", required = true),
//			@ApiImplicitParam(name = "outWarehouseTime", value = "出库时间", dataType = "String", paramType = "query"),
//			@ApiImplicitParam(name = "eum", value = "操作异常清单标志", dataType = "String", paramType = "query", required = true)
//	})
//	public ApiResult<?> addException(ExceptionToolAddDTO dto){
//		checkService.addException(dto);
//		return ApiResult.success();
//	}

}
